home *** CD-ROM | disk | FTP | other *** search
/ Animation How-To / Animation How-to CD.iso / PLY / CHAPTER4 / THREAD / BEZIER.BAS next >
BASIC Source File  |  1994-01-01  |  3KB  |  109 lines

  1. ' BEZIER.BAS
  2.  
  3. DECLARE SUB rotate (x, y, z)
  4. COMMON SHARED rad, xrotate, yrotate, zrotate
  5.  
  6. TYPE Vector
  7.   x AS SINGLE
  8.   y AS SINGLE
  9.   z AS SINGLE
  10. END TYPE
  11.  
  12. DIM p(4) AS Vector
  13.  
  14. pi = 3.1415927#
  15. rad = pi / 180
  16.  
  17. SCREEN 12
  18. WINDOW (-16, -4)-(16, 20)
  19.  
  20. CLS
  21.  
  22. ' our four control points.  These get spiraled
  23. ' down a coil to generate the bezier batches
  24.  
  25. p(1).x = 1: p(1).y = 1: p(1).z = 0
  26. p(2).x = -1: p(2).y = 1: p(2).z = 0
  27. p(3).x = 1: p(3).y = -1: p(3).z = 0
  28. p(4).x = -1: p(4).y = -1: p(4).z = 0
  29.  
  30. ' show a spiral path 2-D
  31.  
  32.    FOR a = 90 TO 90 + 360 * 3
  33.       y = 10 * SIN(a * rad)
  34.       CIRCLE (y, (a / 75) - 1), .1, 1
  35.       PAINT (y, (a / 75) - 1), 15, 1
  36.    NEXT a
  37.  
  38. FOR dat = 0 TO 44   '45 frames
  39.  
  40.    xrotate = 0
  41.    yrotate = 0
  42.    zrotate = 4
  43.  
  44.    'take 4 points in a square and rotate them
  45.    FOR a = 1 TO 4
  46.       CALL rotate(p(a).x, p(a).y, p(a).z)
  47.    NEXT a
  48.    
  49.    r = 10
  50.    ang = 0          ' initial value for ang = 0
  51.    thread = 5 / 360 ' gives 5 units up per 360 degrees
  52.  
  53.    LOCATE 1, 1: PRINT "Frame "; dat
  54.  
  55.    FOR patch = 1 TO 24   ' 24 patches spanning 45 degrees each
  56.       FOR n = 1 TO 4     ' 4 sets of 4 points per patch
  57.          FOR a = 1 TO 4  ' each point
  58.       
  59.             xrotate = 0
  60.             yrotate = ang  ' rotate about y axis
  61.             zrotate = 0
  62.                                        
  63.             tx = p(a).x + r            ' offset sets a circle
  64.             ty = p(a).y + ang * thread ' this gives a spiral
  65.             tz = p(a).z + 0
  66.       
  67.             CALL rotate(tx, ty, tz)
  68.             IF n = 2 OR n = 3 THEN
  69.                tx = tx * 1.04      ' move the middle two control point sets
  70.                tz = tz * 1.04      ' out to make the spiral rounder
  71.             END IF
  72.             CIRCLE (tx, ty), .1, a + k
  73.          NEXT a
  74.  
  75.          ' ang isn't indexed after the last set,
  76.          ' so the bezier ends will overlap
  77.  
  78.          IF n < 4 THEN ang = ang + (45 / 3)
  79.       NEXT n
  80.    NEXT patch
  81. NEXT dat
  82.  
  83. SUB rotate (x, y, z)
  84.  
  85. 'rotate
  86.  
  87.     x0 = x
  88.     y0 = y
  89.     z0 = z
  90.  
  91.     x1 = x0
  92.     y1 = y0 * COS(xrotate * rad) - z0 * SIN(xrotate * rad)
  93.     z1 = y0 * SIN(xrotate * rad) + z0 * COS(xrotate * rad)
  94.  
  95.     x2 = z1 * SIN(yrotate * rad) + x1 * COS(yrotate * rad)
  96.     y2 = y1
  97.     z2 = z1 * COS(yrotate * rad) - x1 * SIN(yrotate * rad)
  98.  
  99.     x3 = x2 * COS(zrotate * rad) - y2 * SIN(zrotate * rad)
  100.     y3 = x2 * SIN(zrotate * rad) + y2 * COS(zrotate * rad)
  101.     z3 = z2
  102.  
  103.     x = x3
  104.     y = y3
  105.     z = z3
  106.  
  107. END SUB
  108.  
  109.